<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('client.trace.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('client.trace.type')" prop="type">
            <el-select v-model="dataForm.type" :client.traceholder="i18n('common.please_select') + i18n('client.trace.type')" clearable>
              <el-option v-for="(v, k) in typeList" :label="v.text" :key="k" :value="v.value"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('client.trace.contact_id')" prop="contact_id">
            <el-select v-model="dataForm.contact_id" :client.traceholder="i18n('common.please_select') + i18n('client.trace.contact_id')" clearable>
              <el-option v-for="(v, k) in contactList" :label="v.name" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('client.trace.content')" prop="content">
            <el-input
              type="textarea"
              v-model="dataForm.content"
              :rows="5"
              show-word-limit
              maxlength="500"
              :exampleholder="i18n('common.please_input') + i18n('client.trace.content')">
            </el-input>
          </el-form-item>

          <el-divider content-position="left">后续追踪提醒</el-divider>

          <el-form-item :label="i18n('client.trace.reminder_date')" prop="reminder_date">
            <el-date-picker v-model="dataForm.reminder_date" type="date" :placeholder="i18n('common.please_input') + i18n('client.trace.reminder_date')" clearable/>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>

            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-client-trace-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/client/trace')

  const typeList = ref([])
  const contactList = ref([])

  const dataForm = reactive({
    id: 0,
    client_id: '',
    contact_id: '',
    type: '',
    content: '',
    reminder_date: '',
  })

  const dataRule = reactive({
    type: [{
      required: true,
      message: i18n('client.trace.rules.type.require'),
      trigger: 'blur'
    }],
    contact_id: [{
      required: true,
      message: i18n('client.trace.rules.contact_id.require'),
      trigger: 'blur'
    }],
    content: [{
      required: true,
      message: i18n('client.trace.rules.content.require'),
      trigger: 'blur'
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.client_id = data.data.client_id
            dataForm.contact_id = data.data.contact_id
            dataForm.type = data.data.type.value + ''
            dataForm.content = data.data.content
            dataForm.reminder_date = data.data.reminder_date
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            client_id: dataForm.client_id,
            contact_id: dataForm.contact_id,
            type: dataForm.type,
            content: dataForm.content,
            reminder_date: dataForm.reminder_date,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }


  const loadTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'contact_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        typeList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  const loadContactList = (client_id) => {
    $http({
      url: $http.adornUrl('/business/contact/select'),
      method: 'get',
      params: $http.adornParams({
        client_id: client_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        contactList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  if(query.client_id) {
    dataForm.client_id = parseInt(query.client_id)
  }

  onMounted(() => {
    init(formRef)

    loadTypeList()
    loadContactList(query.client_id)
  })
</script>
